<?php
/**
 * Created by PhpStorm.
 * User: dayu
 * Date: 2019/12/19
 * Time: 15:54
 */

namespace app\api\controller;

use app\Validate\GoodsValidate;
use function data\extend\alipay\charsetEncode;
use data\model\GoodsVarietyModel;
use data\model\UserModel;
use data\service\GoodsCategory;
use data\service\Goods as GoodsService;
use data\model\NsGoodsModel;
use data\service\GoodsVariety;
use data\service\Member;
use think\Controller;
use data\service\Order\Order;
use data\model\NsMemberExpressAddressModel;
use think\cache\driver\Redis;
use data\service\WxPay;
use data\model\NsPlatformGoodsRecommendModel;
use data\model\NsGoodsBrandModel;
class Goods extends BaseController
{

    /**
     * 商品分类列表
     * @param int pid 分类等级
     * @return string
     * @throws \Exception
     */
    public function goodsCategoryList(GoodsValidate $goodsValidate)
    {
        $goodscate = new GoodsCategory();
        $data['data'] = $goodscate->getGoodsCategoryListByParentId($goodsValidate->request->post("pid"));
        $data['count'] = count($data['data']);
        return return_json_data($data, 200);
    }

    /**
     * 首页3大分类商品列表
     * @param GoodsValidate $goodsValidate
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function HomeGoodsList(GoodsValidate $goodsValidate)
    {
        $GoodsVariety = new  GoodsVariety();
        $type = $goodsValidate->request->post('type');
        $pag = $goodsValidate->request->post('pag', 1);
        $number = $goodsValidate->request->post('number', 10);
        $variety_id_activation = $goodsValidate->request->post('variety_id_activation', '');
        if (empty($variety_id_activation) && $variety_id_activation !== 0) {
            switch ($type) {
                case "1":
                    $variety_id = 42;
                    break;
                case "2":
                    $variety_id = 43;
                    break;
                case "3":
                    $variety_id = 44;
                    break;
            }
        } else {
            $variety_id = $variety_id_activation;
        }

        $data = $GoodsVariety->HomeGoodsList($variety_id, $this->shop_id, $pag, $number, $this->redis, $this->user_id);
        return return_json_data($data, 200);
    }


    /**
     * 自定义分类列表
     * @param GoodsValidate $goodsValidate
     */
    public function VarietyList(GoodsValidate $goodsValidate)
    {
        $GoodsVariety = new  GoodsVariety();
        $level = $goodsValidate->request->post('level', '');
        $pag = $goodsValidate->request->post('pag', 1);
        $number = $goodsValidate->request->post('number', 10);
        $variety_id_activation = $goodsValidate->request->post('variety_id_activation', '');

        $where = $_sort = [];
        $where['type'] = 1;
        $_sort = ['sort' => "desc"];
        if (!$variety_id_activation) {
            $where['place'] = 1;
        } else {
            if (empty($level)) return return_json_data('参数缺失', 500);
        }
        $data = $GoodsVariety->HomeVarietyList($variety_id_activation, $level, $pag, $number, $where, $_sort, 'variety_id,variety_name,image,url,ladder');
        return return_json_data($data, 200);
    }

    /**
     * 猜你喜欢 - 作废
     */
    public function getGuessMemberLikes()
    {
        $pag = request()->post('pag', 1);
        $number = request()->post('number', 10);

        echo $cache = "cacheHotSale" . $this->user_id;
        $data = $this->redis->get($cache);

        if (empty($data)) {
            $data = $this->redis->zrange("HotSale" . $this->user_id, 0, -1);
            shuffle($data);
            $data = implode(',', $data);
            $boole = $this->redis->set($cache, $data);
        }
        $data = explode(',', $data);
        $data = array_slice($data, ($pag - 1) * $number, $number);
        return return_json_data($data, 200);
    }

    /**
     * 内购商品
     */
    public function purchasing()
    {
        $goods = new Goods();

        $boole = (new Redis())->get(request()->header("access-token"));
        $goods->GoodsInfo();
        var_dump($boole);

    }

    /**
     * 商品信息获取
     * @param GoodsValidate $goodsValidate
     */
    public function getGoodsInfo(GoodsValidate $goodsValidate)
    {
        $goods = new GoodsService();
        $goods_id = $goodsValidate->request->post('goods_id');
        $where = ['state' => 1];
        $data = $goods->getBasisGoodsDetail($goods_id, $where, true, $this->redis, $this->shop_id, $this->user_id);
        return return_json_data($data, 200);
    }


    public function goodsVariety()
    {

        $Variety = new GoodsVarietyModel();
        $data = $Variety->select();
        return return_json_data($data, 200);

    }

    /**
     * 商品订单创建
     * @param GoodsValidate $goodsValidate
     */
    public function createGoodsOrder(GoodsValidate $goodsValidate)
    {
        $db = new Order();
        //订单类型
        $order_type = $goodsValidate->request->post("order_type");
        //订单号
        $out_trade_no = createOutRradeNo($order_type);

        //订单sku列表
        $goods_sku_list = $goodsValidate->request->post("goods_sku_list");

        $addres_id = $goodsValidate->request->post("addres_id");

        //买家附言
        $buyer_message = $goodsValidate->request->post("buyer_message", '');

        //订单代金券id
        $coupon_id = $goodsValidate->request->post("coupon_id", '');

        //配送物流公司ID
        $shipping_company_id = '';
        $boole = $db->orderCreate($addres_id, 1, $out_trade_no, $buyer_message, $coupon_id, json_decode($goods_sku_list, true), $shipping_company_id, $this->user_id);

        if ($boole !== true) {
            return return_json_data("订单创建失败", 500);
        }
        return return_json_data("订单创建成功");
    }

    /**
     * 添加购物车，1：允许购买，0：不允许购买
     *
     * @return string
     * @throws \Exception
     */
    public function addCart()
    {
        $shop_id = request()->post('shop_id', '');
        $shop_name = request()->post('shop_name', '');
        $goods_id = request()->post('trueId', '');
        $goods_name = request()->post('goods_name', '');
        $num = request()->post('count', '');
        $sku_id = request()->post('select_skuid', '');
        $sku_name = request()->post('select_skuName', '');
        $price = request()->post('price', '');
        $picture = request()->post('picture', '');
        $uid = request()->post('uid', '');

        $member = new Member();
        $is_member = $member->getIsMember($uid);
        if (!empty($uid) && $is_member == 1) {
            $goods = new GoodsService();
            $retval = $goods->addCart($uid, $shop_id, $shop_name, $goods_id, $goods_name, $sku_id, $sku_name, $price, $num, $picture, 0);
        } else {
            $retval = array(
                "code" => -1,
                "message" => ""
            );
        }
        return return_json_data($retval);
    }

    /**
     * 购物车修改数量
     */
    public function cartAdjustNum()
    {
        if (request()->isAjax()) {
            $cart_id = request()->post('cartid', '');
            $num = request()->post('num', '');
            $goods = new GoodsService();
            $retval = $goods->cartAdjustNum($cart_id, $num);
            return AjaxReturn($retval);
        } else
            return AjaxReturn(-1);
    }

    /**
     * 购物车项目删除
     */
    public function cartDelete()
    {
        if (request()->isAjax()) {
            $cart_id_array = request()->post('del_id', '');
            $goods = new GoodsService();
            $retval = $goods->cartDelete($cart_id_array);
            return AjaxReturn($retval);
        } else
            return AjaxReturn(-1);
    }

    /**
     * 购物车显示
     *
     * @return array|mixed|null
     * @author dzl
     */
    public function getShoppingCart()
    {
        $uid = request()->post('uid', '');
        $goods = new GoodsService();
        $cart_list = $goods->getCart($uid);
        return $cart_list;
    }

    /**
     * 热门分类
     */
    public function PopularRecommend()
    {
        $goods = new GoodsService();
        $retval = $goods->PopularRecommend($this->shop_id, 1, 6);
        return return_json_data($retval);
    }

    /**
     * 热门分类商品
     */
    public function RecommendGoods(GoodsValidate $goodsValidate)
    {
        $goods = new GoodsService();
        $category_id = $goodsValidate->request->post('category_id');
        $pag = $goodsValidate->request->post('pag', 1);
        $number = $goodsValidate->request->post('number', 10);
        $brand_id = $goodsValidate->request->post('brand_id', '');
        $order = $goodsValidate->request->post('order');
        if($order==5&&empty($brand_id)){
            return return_json_data('品牌参数缺失',500);
        }
        $retval = $goods->RecommendGoods($category_id, $this->shop_id, $pag, $number, $order,$brand_id);
        return return_json_data($retval);
    }

    /**
     * 品牌名列表
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function GoodsBrand()
    {
        $brand = new NsGoodsBrandModel();
        $data['count'] = $brand->where('shop_id','in',[0,$this->shop_id])->count();
        $data['data'] = $brand->where('shop_id','in',[0,$this->shop_id])->field("brand_id,brand_name")->select();
        return return_json_data($data);
    }

    /**
     * 自定义分类集合
     */
    public function VarietyGather(GoodsValidate $goodsValidate)
    {
        $goods = new GoodsService();
        $variety_id = $goodsValidate->request->post('variety_id');
        $where = ['type'=>1];
        $category_id = $goods->VarietyGather($this->shop_id,$variety_id, 2, 1, 3, $where, ['variety_id'=>'desc'], 'variety_id,variety_name,image,url,ladder');
        return return_json_data($category_id);

    }

}